package com.cheer.ba.model;

import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.choosepic.ui.GalleryActivity;
import com.cheer.ba.choosepic.util.Bimp;
import com.cheer.ba.choosepic.util.ImageItem;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.widget.MultiImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论列表adpter
 * Created by Hq on 2017/7/4.
 */
public class CommentAdp extends BaseQuickAdapter<CommentModel, BaseViewHolder> {

    private int width;

    public CommentAdp(@LayoutRes int layoutResId, @Nullable List<CommentModel> data) {
        super(layoutResId, data);
    }

    public CommentAdp(@LayoutRes int layoutResId, @Nullable List<CommentModel> data, int width) {
        super(layoutResId, data);
        this.width = width;
    }

    @Override
    protected void convert(BaseViewHolder helper, CommentModel item) {
        if (null != item) {
            LinearLayout mLinearReply = helper.getView(R.id.id_reply_linear);
            ImageView mUserImg = helper.getView(R.id.id_comment_head_img);
            MultiImageView mPicView = helper.getView(R.id.id_comment_pic_list);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width * 2 / 3, LinearLayout.LayoutParams.WRAP_CONTENT);
            mPicView.setLayoutParams(params);

            LinearLayout mLinearReplyNum = helper.getView(R.id.id_look_reply_linear);
            //1评论 2回复
            int cmtType = item.cmtType;
            String custId = "";
            if (cmtType == 1) {
                mLinearReply.setVisibility(View.INVISIBLE);
                mPicView.setVisibility(View.VISIBLE);
                mLinearReplyNum.setVisibility(View.VISIBLE);
                helper.setText(R.id.id_comment_num_txt, "回复(" + item.replyCount + ")");
                helper.addOnClickListener(R.id.id_comment_rela);

                ImageUtils.showCircleImg(mContext, Protect.setPic(item.custImg), R.drawable.ic_user_head, R.drawable.ic_user_head, mUserImg);
                helper.setText(R.id.id_comment_name_txt, item.custNname);

                custId = item.custId;
            } else {
                //回复
                helper.addOnClickListener(R.id.id_click_reply_linear);
                mLinearReply.setVisibility(View.VISIBLE);
                mPicView.setVisibility(View.GONE);
                mLinearReplyNum.setVisibility(View.GONE);

                ImageUtils.showCircleImg(mContext, Protect.setPic(item.custImg), R.drawable.ic_user_head, R.drawable.ic_user_head, mUserImg);
                helper.setText(R.id.id_comment_name_txt, item.custNname);
                ImageUtils.showCircleImg(mContext, Protect.setPic(item.replyCustImg), R.drawable.ic_user_head, R.drawable.ic_user_head, (ImageView) helper.getView(R.id.id_circle_reply_img));
                helper.setText(R.id.id_circle_reply_name_txt, item.replyCustNname);

                custId = item.custId;
            }

            if (TextUtils.equals(Session.getUserId(), custId)) {
                helper.getView(R.id.id_comment_del_txt).setVisibility(View.VISIBLE);
                helper.addOnClickListener(R.id.id_comment_del_txt);
            } else {
                helper.getView(R.id.id_comment_del_txt).setVisibility(View.GONE);
            }
            helper.setText(R.id.id_comment_time_txt, StringUtils.getDetailStr(item.releaseDate));
            helper.setText(R.id.id_comment_content_txt, item.context);

            final List<OilDetailBannerModel> mPics = item.picList;
            if (null != mPics && !mPics.isEmpty()) {
                OilDetailBannerModel model = mPics.get(0);
                if (model.fileType == 1) {
                    mPicView.setVisibility(View.VISIBLE);
                    mPicView.setList(mPics,1);
                    mPicView.setOnItemClickListener(new MultiImageView.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position,int fileType, String videoUrl) {
                            //图片
                            Bimp.tempSelectBitmap.clear();
                            for (OilDetailBannerModel model : mPics) {
                                ImageItem item = new ImageItem();
                                item.setPicUrl(Protect.setPic(model.fileUrl));
                                item.setFileStatus(model.fileStatus);
                                item.setDrawable(R.drawable.ic_pl_pic_bg);
                                Bimp.tempSelectBitmap.add(item);
                            }
                            Intent intent = new Intent(mContext, GalleryActivity.class);
                            intent.putExtra("position", "1");
                            intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_PIC);
                            intent.putExtra("ID", position);
                            mContext.startActivity(intent);
                        }
                    });
                } else {
                    mPicView.setVisibility(View.GONE);
                }
            } else {
                mPicView.setVisibility(View.GONE);
            }
        }
    }
}
